﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Core.Data;
using EzDesk.Core.Model;
using Microsoft.Practices.ObjectBuilder2;

namespace EzDesk.Core.ServiceImpl.Services
{
    public static class FeedModelExt
    {
        public static IList<Feed> ExtendFeedModelForUser(this IList<Feed> feedList,
            Guid userId, int? feedTypeValue,
            int? startIndex, int? count,
            bool? isRead, bool? isAvailable, bool? isMark, EzDeskDatabaseContainer container)
        {
            feedList.ForEach(item =>
                              {
                                  var feedRecipient =
                                      container.Feed_RecipientSet.FirstOrDefault(
                                          p => p.FeedId == item.Id && p.RecipientId == userId);
                                  if (feedRecipient != null)
                                  {
                                      item.FeedType = feedRecipient.FeedTypeValue;
                                      item.LastChangeOn = feedRecipient.LastChangeOn;
                                      item.IsRead = feedRecipient.IsRead;
                                      item.IsAvailable = feedRecipient.IsAvailable;
                                      item.IsMark = feedRecipient.IsMark;
                                  }
                              });
            var query = feedList.AsQueryable();



            if (feedTypeValue.HasValue)
            {
                query = query.Where(p => p.FeedType == feedTypeValue.Value);
            }
            if (isRead.HasValue)
            {
                query = query.Where(p => p.IsRead == isRead.Value);
            }
            if (isAvailable.HasValue)
            {
                query = query.Where(p => p.IsAvailable == isAvailable.Value);
            }
            if (isMark.HasValue)
            {
                query = query.Where(p => p.IsMark == isMark.Value);
            }

            if (startIndex.HasValue && count.HasValue)
            {
                query = query.Skip(startIndex.Value).Take(count.Value);
            }
            return query.ToList();
        }

        public static IList<Feed> ExtendFeedModel(this IList<Feed_Recipient> feedRecipientList, EzDeskDatabaseContainer container)
        {
            IList<Feed> feedList = new List<Feed>();
            feedRecipientList.ForEach(
                item =>
                    {
                        container.LoadProperty(item, p => p.Feed);
                        var feedItem = item.Feed;
                        container.LoadProperty(feedItem, p => p.FeedTemplate);
                        feedItem.IconUrl = feedItem.FeedTemplate.TemplateIconName;
                        feedItem.FeedType = item.FeedTypeValue;
                        feedItem.LastChangeOn = item.LastChangeOn;
                        feedItem.IsAvailable = item.IsAvailable;
                        feedItem.IsRead = item.IsRead;
                        feedItem.IsMark = item.IsMark;
                        feedList.Add(feedItem);
                    });
            return feedList;
        }

        public static IList<Feed> ExtendFeedModelForUser(this IList<Feed> originalFeedList,
            Guid userId, int? feedTypeValue,
            bool? isRead, bool? isAvailable, bool? isMark, EzDeskDatabaseContainer container)
        {
            IList<Feed> feedList = new List<Feed>();
            originalFeedList.ForEach(item =>
            {
                var query =
                    container.Feed_RecipientSet.Where(
                        p => p.FeedId == item.Id && p.RecipientId == userId);

                if (feedTypeValue.HasValue)
                {
                    query = query.Where(p => p.FeedTypeValue == feedTypeValue.Value);
                }
                if (isRead.HasValue)
                {
                    query = query.Where(p => p.IsRead == isRead.Value);
                }
                if (isAvailable.HasValue)
                {
                    query = query.Where(p => p.IsAvailable == isAvailable.Value);
                }
                if (isMark.HasValue)
                {
                    query = query.Where(p => p.IsMark == isMark.Value);
                }

                var feedRecipient = query.FirstOrDefault();

                if (feedRecipient != null)
                {
                    item.FeedType = feedRecipient.FeedTypeValue;
                    item.LastChangeOn = feedRecipient.LastChangeOn;
                    item.IsRead = feedRecipient.IsRead;
                    item.IsAvailable = feedRecipient.IsAvailable;
                    item.IsMark = feedRecipient.IsMark;
                    feedList.Add(item);
                }
            });

            return feedList;
        }

    }

}
